package com.yigerende.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.yigerende.model.Blog;
import com.yigerende.model.Type;
import com.yigerende.service.BlogService;
import com.yigerende.service.TypeService;
import com.yigerende.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


import java.util.List;


@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                   @PathVariable Long id,Pageable pageable, Model model) {
        //获取所有分类
        List<Type> types=typeService.listTypeTop(10000);//显示的条数，给一个很大的数字这样就不会超过有的数据

        //博客首页点击more进来的
        if (id == -1) {//首页点分类传递一个-1过来
            //自动选择第一个分类，显示其下的博客
            id = types.get(0).getId();//默认显示第一个分类的数据，取到第一个给id
        }

        //也使用那个后台的博客的选择性搜索，里面就有分类搜索博客
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        model.addAttribute("types", types);//所有分类
        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));
        //判断选中和未选中，要把那个数据再传递回去
        model.addAttribute("activeTypeId",id);
        return "types";
    }
}
